package com.qiuling.iot.rabbit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiuling.iot.Exception.code.FunctionCodeEnum;
import com.qiuling.iot.config.RabbitConfig;
import com.qiuling.iot.utils.GPRSUtils;
import com.qiuling.iot.vo.req.ClientSendReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 1:42 2020/3/6
 * @Modified By：
 */
@Component
@Slf4j
public class MessageProduct {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private GPRSUtils gprsUtils;
    @Autowired
    private ObjectMapper objectMapper;


    ///**
    // * @description 发送心跳码
    // * </br>
    // * @param url
    // * @param equipmentCode
    // * @return com.qiuling.iot.vo.req.ClientSendReqVO
    // * @author zhangqiang
    // * @date 0:19 2020/3/10
    // */
    //public ClientSendReqVO sendHeratCode(String url, String equipmentCode) {
    //    ClientSendReqVO sendReqVO = gprsUtils.sendHeratCode(url, equipmentCode);
    //    if(sendReqVO != null) {
    //        rabbitTemplate.setExchange(RabbitConfig.IOT_EXCHANGE);
    //        rabbitTemplate.setRoutingKey(RabbitConfig.DATA_ROUTING_KEY);
    //        try {
    //            rabbitTemplate.convertAndSend(MessageBuilder.withBody(objectMapper.writeValueAsBytes(sendReqVO))
    //                    .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT));
    //        } catch (JsonProcessingException e) {
    //            log.error("json解析失败,{}", e.getMessage());
    //        }
    //    }
    //
    //    return sendReqVO;
    //}

    //测试用
    public ClientSendReqVO sendHeratCode() {
        ClientSendReqVO sendReqVO = new ClientSendReqVO();
        sendReqVO.setEquipmentCode("IOT0000005");
        sendReqVO.setFunctionCode(FunctionCodeEnum.HEART.getValue());
        if(sendReqVO != null) {
            rabbitTemplate.setExchange(RabbitConfig.IOT_EXCHANGE);
            rabbitTemplate.setRoutingKey(RabbitConfig.DATA_ROUTING_KEY);
            try {
                rabbitTemplate.convertAndSend(MessageBuilder.withBody(objectMapper.writeValueAsBytes(sendReqVO))
                        .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT));
            } catch (JsonProcessingException e) {
                log.error("json解析失败,{}", e.getMessage());
            }
        }

        return sendReqVO;
    }


}
